import type { AttributeType } from '../../model/attributes/type';
import type { Condition } from '../../systems/condition-checker/type';
import type { AuthorInfo } from '../../types';

/** 物品类型 */
export const ITEM_TYPE = {
  /** 功法 */
  CULTIVATION_SKILL: 'CULTIVATION_SKILL',
  /** 装备 */
  EQUIPMENT: 'EQUIPMENT',
  /** 材料 */
  MATERIAL: 'MATERIAL',
} as const;

export type ItemType = (typeof ITEM_TYPE)[keyof typeof ITEM_TYPE];

export const ITEM_TYPE_DESC_MAP: Record<ItemType, string> = {
  [ITEM_TYPE.CULTIVATION_SKILL]: '功法',
  [ITEM_TYPE.EQUIPMENT]: '装备',
  [ITEM_TYPE.MATERIAL]: '材料',
};

export const ITEM_TYPE_ORDER = [
  ITEM_TYPE.EQUIPMENT,
  ITEM_TYPE.CULTIVATION_SKILL,
  ITEM_TYPE.MATERIAL,
];

/** 品级 */
export const ITEM_RARITY = {
  /** 凡品 */
  COMMON: 'COMMON',
  /** 下品 */
  INFERIOR: 'INFERIOR',
  /** 中品 */
  MEDIUM: 'MEDIUM',
  /** 上品 */
  SUPERIOR: 'SUPERIOR',
  /** 极品 */
  SUPREME: 'SUPREME',
  /** 仙品 */
  IMMORTAL: 'IMMORTAL',
  /** 完美 */
  PERFECT: 'PERFECT',
  /** 先天 */
  INNATE: 'INNATE',
  /** 至宝 */
  TREASURE: 'TREASURE',
} as const;

export type ItemRarity = (typeof ITEM_RARITY)[keyof typeof ITEM_RARITY];

export const ITEM_RARITY_DESC_MAP: Record<ItemRarity, string> = {
  [ITEM_RARITY.COMMON]: '凡品',
  [ITEM_RARITY.INFERIOR]: '下品',
  [ITEM_RARITY.MEDIUM]: '中品',
  [ITEM_RARITY.SUPERIOR]: '上品',
  [ITEM_RARITY.SUPREME]: '极品',
  [ITEM_RARITY.IMMORTAL]: '仙品',
  [ITEM_RARITY.PERFECT]: '完美',
  [ITEM_RARITY.INNATE]: '先天',
  [ITEM_RARITY.TREASURE]: '至宝',
};

export const ITEM_RARITY_ORDER: ItemRarity[] = [
  ITEM_RARITY.COMMON,
  ITEM_RARITY.INFERIOR,
  ITEM_RARITY.MEDIUM,
  ITEM_RARITY.SUPERIOR,
  ITEM_RARITY.SUPREME,
  ITEM_RARITY.IMMORTAL,
  ITEM_RARITY.PERFECT,
  ITEM_RARITY.INNATE,
  ITEM_RARITY.TREASURE,
];

export interface BaseItemConfig {
  id: string;
  name: string;
  /** 作者信息 */
  author?: AuthorInfo;
  /** 物品类型 */
  type: ItemType;
  /** 物品品级 */
  rarity?: ItemRarity;
  /** 物品最大叠加数量（0 为无限） */
  maxStack: number;
  /** 基础出售价格 */
  basePrice: number;
  /** 能否出售（默认 true） */
  canSell?: boolean;
}

/** 功法熟练度等级 */
export const SKILL_MASTERY_LEVEL = {
  /** 入门 */
  BEGINNER: 'BEGINNER',
  /** 小成 */
  MINOR_ACHIEVEMENT: 'MINOR_ACHIEVEMENT',
  /** 大成 */
  MAJOR_ACHIEVEMENT: 'MAJOR_ACHIEVEMENT',
  /** 圆满 */
  PERFECTION: 'PERFECTION',
  /** 化境 */
  SUBLIME: 'SUBLIME',
} as const;

export const SKILL_MASTERY_LEVEL_MAP = {
  [SKILL_MASTERY_LEVEL.BEGINNER]: '入门',
  [SKILL_MASTERY_LEVEL.MINOR_ACHIEVEMENT]: '小成',
  [SKILL_MASTERY_LEVEL.MAJOR_ACHIEVEMENT]: '大成',
  [SKILL_MASTERY_LEVEL.PERFECTION]: '圆满',
  [SKILL_MASTERY_LEVEL.SUBLIME]: '化境',
} as const;

export type SkillMasteryLevel =
  (typeof SKILL_MASTERY_LEVEL)[keyof typeof SKILL_MASTERY_LEVEL];

/** 功法等级配置 */
export interface CultivationSkillLevelConfig {
  /** 等级（1-25） */
  level: number;
  /** 对应的熟练度等级 */
  masteryLevel: SkillMasteryLevel;
  /** 升级所需经验值 */
  requiredExp: number;
  /** 修炼速度加成 */
  cultivationSpeedBonus: number;
  /** 突破到下一级所需的特殊条件 */
  breakthroughCondition?: Condition[];
}
export interface CultivationSkillItemConfig extends BaseItemConfig {
  type: typeof ITEM_TYPE.CULTIVATION_SKILL;
  /** 基础修炼速度 */
  baseCultivationSpeed: number;
  /** 功法等级配置 */
  levels: CultivationSkillLevelConfig[];
  /** 最大等级(默认根据 levels 计算) */
  maxLevel: number;
  /** 学习条件 */
  studyConditions: Condition[];
}

export const EQUIP_TYPE = {
  /** 武器 */
  WEAPON: 'WEAPON',
  /** 头部 */
  HEAD: 'HEAD',
  /** 衣服 */
  BODY: 'BODY',
  /** 腰带 */
  BELT: 'BELT',
  /** 鞋履 */
  SHOES: 'SHOES',
  /** 护腕 */
  WRIST: 'WRIST',
  /** 宝珠 */
  BEAD: 'BEAD',
  /** 项链 */
  NECKLACE: 'NECKLACE',
  /** 法器 */
  MAGICAL_INSTRUMENT: 'MAGICAL_INSTRUMENT',
  /** 秘宝 */
  TREASURE: 'TREASURE',
} as const;

export type EquipType = (typeof EQUIP_TYPE)[keyof typeof EQUIP_TYPE];

export const EQUIP_SLOT = {
  /** 武器 */
  WEAPON: 'WEAPON',
  /** 头部 */
  HEAD: 'HEAD',
  /** 衣服 */
  BODY: 'BODY',
  /** 腰带 */
  BELT: 'BELT',
  /** 鞋履 */
  SHOES: 'SHOES',
  /** 护腕 */
  WRIST: 'WRIST',
  /** 宝珠 */
  BEAD: 'BEAD',
  /** 项链 */
  NECKLACE: 'NECKLACE',
  /** 法器 */
  MAGICAL_INSTRUMENT: 'MAGICAL_INSTRUMENT',
  /** 秘宝 */
  TREASURE: 'TREASURE',
} as const;

export type EquipSlot = (typeof EQUIP_SLOT)[keyof typeof EQUIP_SLOT];

export const EQUIP_SLOT_DESC_MAP: Record<EquipSlot, string> = {
  [EQUIP_SLOT.WEAPON]: '武器',
  [EQUIP_SLOT.HEAD]: '头部',
  [EQUIP_SLOT.BODY]: '衣服',
  [EQUIP_SLOT.BELT]: '腰带',
  [EQUIP_SLOT.SHOES]: '鞋履',
  [EQUIP_SLOT.WRIST]: '护腕',
  [EQUIP_SLOT.BEAD]: '宝珠',
  [EQUIP_SLOT.NECKLACE]: '项链',
  [EQUIP_SLOT.MAGICAL_INSTRUMENT]: '法器',
  [EQUIP_SLOT.TREASURE]: '秘宝',
};

export interface AttrRoll {
  prop: AttributeType;
  base: number;
}

export interface EquipmentItemConfig extends BaseItemConfig {
  type: typeof ITEM_TYPE.EQUIPMENT;
  equipType: EquipType;
  equipSlot: EquipSlot;
  /** 基础属性加成 */
  baseAttr: AttrRoll[];
  /** 装备穿戴条件 */
  equipConditions: Condition[];
}

export interface MaterialItemConfig extends BaseItemConfig {
  type: typeof ITEM_TYPE.MATERIAL;
}
